rm(list=ls())

load(directory of Res_figures)

###########
#################### display
###########



#####    Figure 2

pdf('/Users/Zhichao/Desktop/realdata.pdf',height=4,width=8)



# par(mfrow=c(1,3),widths=c(2,1,1))

nf=layout(matrix(c(1,2,3),1,3), widths=c(1.3,1,1), TRUE)
# layout.show(nf)


### Turnout  history
par(mar=c(2, 6, 2, 0.5))

a11=0.5
a12=0.55
a13=0.6
a14=0.65

plot(ce.rho0.mean[6],a11,'p',pch=15,cex=1,ylim=c(0,1.1),ylab='',xlab='',xlim=c(-0.35,0.45),yaxt='n',main='Turnout history')
points(ce.rho0.ci[,6],rep(a11,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,6],c(a11,a11),col='black')

points(ce.rho0.mean[11],a12,pch=16,cex=1)
points(ce.rho0.ci[,11],rep(a12,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,11],rep(a12,2),lty=3,col='black')

points(ce.rho0.mean[16],a13,pch=17,cex=1)
points(ce.rho0.ci[,16],rep(a13,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,16],rep(a13,2),lty=1,col='grey')

points(ce.rho0.mean[21],a14,pch=18,cex=1)
points(ce.rho0.ci[,21],rep(a14,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,21],rep(a14,2),lty=3,col='grey')

for (i in c(2, 4)){
	p=   2*i-6
	a1=a11+0.2*p
	a2=a12+0.2*p
	a3=a13+0.2*p
	a4=a14+0.2*p
	points(ce.rho0.mean[6+i],a1,pch=15,cex=1)
points(ce.rho0.ci[,6+i],rep(a1,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,6+i],c(a1,a1),col='black')

points(ce.rho0.mean[11+i],a2,pch=16,cex=1)
points(ce.rho0.ci[,11+i],rep(a2,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,11+i],rep(a2,2),lty=3,col='black')

points(ce.rho0.mean[16+i],a3,pch=17,cex=1)
points(ce.rho0.ci[,16+i],rep(a3,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,16+i],rep(a3,2),lty=1,col='grey')

points(ce.rho0.mean[21+i],a4,pch=18,cex=1)
points(ce.rho0.ci[,21+i],rep(a4,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,21+i],rep(a4,2),lty=3,col='grey')
}
abline(v=0)
axis(2,at=c((a11+a14)/2+((0:2)-1)*0.4+0.025,(a11+a14)/2+((0:2)-1)*0.4-0.025),labels=c( paste('Direct ',' ',' '), paste('Contagion ',''),'Spillover ',expression(paste('effect (',eta,'(1))',sep='')),expression(paste('effect (',tau,'(1)) ',sep='')),expression(paste('effect (',theta,') ',sep='') )  ),tick=F,las=2)

text(c(0.35,0.35,-0.2,-0.2),c(a14,a13,a12,a11)+0.4, labels=c('N/N','N/Y','Y/N','Y/Y'))

 # legend(0.04, 1.2, c('NN','NY','YN','YY'), col=c('grey','grey','black','black'), lty = c(3,1,3,1),
       # pch = c(18,17,16,15), ncol = 2, cex = 0.8)

##### partisanship
par(mar=c(2, 1, 2, 0.5))
plot(ce.rho0.mean[46],a11,'p',pch=15,cex=1,ylim=c(0,1.1),ylab='',xlab='',xlim=c(-0.35,0.45),yaxt='n',main='Partisanship')
points(ce.rho0.ci[,46],rep(a11,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,46],c(a11,a11),col='black')

points(ce.rho0.mean[51],a12,pch=16,cex=1)
points(ce.rho0.ci[,51],rep(a12,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,51],rep(a12,2),lty=3,col='black')

points(ce.rho0.mean[56],a13,pch=17,cex=1)
points(ce.rho0.ci[,56],rep(a13,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,56],rep(a13,2),lty=1,col='grey')

points(ce.rho0.mean[61],a14,pch=18,cex=1)
points(ce.rho0.ci[,61],rep(a14,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,61],rep(a14,2),lty=3,col='grey')

for (i in c(2,4)){
	p=   2*i-6
	a1=a11+0.2*p
	a2=a12+0.2*p
	a3=a13+0.2*p
	a4=a14+0.2*p
	points(ce.rho0.mean[46+i],a1,pch=15,cex=1)
points(ce.rho0.ci[,46+i],rep(a1,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,46+i],c(a1,a1),col='black')

points(ce.rho0.mean[51+i],a2,pch=16,cex=1)
points(ce.rho0.ci[,51+i],rep(a2,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,51+i],rep(a2,2),lty=3,col='black')

points(ce.rho0.mean[56+i],a3,pch=17,cex=1)
points(ce.rho0.ci[,56+i],rep(a3,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,56+i],rep(a3,2),lty=1,col='grey')

points(ce.rho0.mean[61+i],a4,pch=18,cex=1)
points(ce.rho0.ci[,61+i],rep(a4,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,61+i],rep(a4,2),lty=3,col='grey')
}
abline(v=0)
# axis(2,at=(a11+a14)/2+(0:2)*0.4,labels=c(expression(paste(tau,'(1)')),expression(paste(eta,'(1)')),expression(theta)   ),tick=F)

text(c(0.35,0.35,-0.2,-0.2),c(a14,a13,a12,a11)+0.4, labels=c('R/R','R/D','D/R','D/D'))
# legend(0.04, 1.2, c('RR','RD','DR','DD'), col=c('grey','grey','black','black'), lty = c(3,1,3,1),
       # pch = c(18,17,16,15), ncol = 2, cex = 0.8)
       
       
 ###  Gender

  par(mar=c(2, 1, 2, 0.5))
  plot(ce.rho0.mean[26],a11,'p',pch=15,cex=1,ylim=c(0,1.1),ylab='',xlab='',xlim=c(-0.35,0.45),yaxt='n',main='Gender')
points(ce.rho0.ci[,26],rep(a11,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,26],c(a11,a11),col='black')

points(ce.rho0.mean[31],a12,pch=16,cex=1,xlim=c(0,2))
points(ce.rho0.ci[,31],rep(a12,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,31],rep(a12,2),lty=3,col='black')

points(ce.rho0.mean[36],a13,pch=17,cex=1,xlim=c(0,2))
points(ce.rho0.ci[,36],rep(a13,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,36],rep(a13,2),lty=1,col='grey')

points(ce.rho0.mean[41],a14,pch=18,cex=1,xlim=c(0,2))
points(ce.rho0.ci[,41],rep(a14,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,41],rep(a14,2),lty=3,col='grey')
for (i in c(2,4)){
	p=   2*i-6
	a1=a11+0.2*p
	a2=a12+0.2*p
	a3=a13+0.2*p
	a4=a14+0.2*p
	points(ce.rho0.mean[26+i],a1,pch=15,cex=1,ylim=c(0,1.2))
points(ce.rho0.ci[,26+i],rep(a1,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,26+i],c(a1,a1),col='black')

points(ce.rho0.mean[31+i],a2,pch=16,cex=1,ylim=c(0,1.2))
points(ce.rho0.ci[,31+i],rep(a2,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,31+i],rep(a2,2),lty=3,col='black')

points(ce.rho0.mean[36+i],a3,pch=17,cex=1,ylim=c(0,1.2))
points(ce.rho0.ci[,36+i],rep(a3,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,36+i],rep(a3,2),lty=1,col='grey')

points(ce.rho0.mean[41+i],a4,pch=18,cex=1,ylim=c(0,1.2))
points(ce.rho0.ci[,41+i],rep(a4,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,41+i],rep(a4,2),lty=3,col='grey')
}
abline(v=0)
# axis(2,at=(a11+a14)/2+(0:2)*0.4,labels=c(expression(paste(tau,'(1)')),expression(paste(eta,'(1)')),expression(theta)   ),tick=F)

text(c(0.35,0.35,-0.2,-0.2),c(a14,a13,a12,a11)+0.4, labels=c('F/F','F/M','M/F','M/M'))
# legend(0.04, 1.2, c('FF','FM','MF','MM'), col=c('grey','grey','black','black'), lty = c(3,1,3,1),
       # pch = c(18,17,16,15), ncol = 2, cex = 0.8)
dev.off()





###################################  Figure 3
pdf('/Users/Zhichao/Desktop/realdata_sens.pdf',height=6,width=8)






nf=layout(matrix(c(1,3,2,4),2,2), widths=c(1.02,1)*4/3,heights=c(1,1), TRUE)
# layout.show(nf)
######  DR  rho
par(mar=c(2, 4.5, 2, 0.5))

a11=-0.5
a12=-0.25
a13=0
a14=0.25
a15=0.5

a16=-0.75
a17=0.75

plot(ce.rhon75.mean[51],a16,'p',pch=16,cex=1,ylim=c(-0.8,0.8),ylab=expression(rho),xlab='',xlim=c(-0.05,0.5),yaxt='n',main='Party: Democrat/Republican (D/R)',cex.lab=1.5)
points(ce.rhon75.ci[,51],rep(a16,2),pch='|',cex=0.5)
lines(ce.rhon75.ci[,51],c(a16,a16),lty=1,col='black')


points(ce.rho75.mean[51],a17,pch=16,cex=1)
points(ce.rho75.ci[,51],rep(a17,2),pch='|',cex=0.5)
lines(ce.rho75.ci[,51],c(a17,a17),lty=1,col='black')

points(ce.rhon50.mean[51],a11,pch=16,cex=1)
points(ce.rhon50.ci[,51],rep(a11,2),pch='|',cex=0.5)
lines(ce.rhon50.ci[,51],c(a11,a11),lty=1,col='black')

points(ce.rhon25.mean[51],a12,pch=16,cex=1)
points(ce.rhon25.ci[,51],rep(a12,2),pch='|',cex=0.5)
lines(ce.rhon25.ci[,51],rep(a12,2),lty=1,col='black')

points(ce.rho0.mean[51],a13,pch=16,cex=1)
points(ce.rho0.ci[,51],rep(a13,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,51],rep(a13,2),lty=1,col='black')

points(ce.rho25.mean[51],a14,pch=16,cex=1)
points(ce.rho25.ci[,51],rep(a14,2),pch='|',cex=0.5)
lines(ce.rho25.ci[,51],rep(a14,2),lty=1,col='black')

points(ce.rho50.mean[51],a15,pch=16,cex=1)
points(ce.rho50.ci[,51],rep(a15,2),pch='|',cex=0.5)
lines(ce.rho50.ci[,51],rep(a15,2),lty=1,col='black')
abline(v=0,lty=3)
axis(2,at=c(-0.75,-0.5,-0.25,0,0.25,0.5,0.75),labels=c('-0.75','-0.5','-0.25','0','0.25','0.5' ,'0.75'),tick=T)


#######     FM  rho 


par(mar=c(2, 4, 2, 0.5))



plot(ce.rhon75.mean[36],a16,'p',pch=16,cex=1,ylim=c(-0.8,0.8),ylab='',xlab='',xlim=c(-0.05,0.5),yaxt='n',main='Gender: Female/Male (F/M)')
points(ce.rhon75.ci[,36],rep(a16,2),pch='|',cex=0.5)
lines(ce.rhon75.ci[,36],c(a16,a16),lty=1,col='black')


points(ce.rho75.mean[36],a17,pch=16,cex=1)
points(ce.rho75.ci[,36],rep(a17,2),pch='|',cex=0.5)
lines(ce.rho75.ci[,36],c(a17,a17),lty=1,col='black')

points(ce.rhon50.mean[36],a11,pch=16,cex=1)
points(ce.rhon50.ci[,36],rep(a11,2),pch='|',cex=0.5)
lines(ce.rhon50.ci[,36],c(a11,a11),lty=1,col='black')

points(ce.rhon25.mean[36],a12,pch=16,cex=1)
points(ce.rhon25.ci[,36],rep(a12,2),pch='|',cex=0.5)
lines(ce.rhon25.ci[,36],rep(a12,2),lty=1,col='black')

points(ce.rho0.mean[36],a13,pch=16,cex=1)
points(ce.rho0.ci[,36],rep(a13,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,36],rep(a13,2),lty=1,col='black')

points(ce.rho25.mean[36],a14,pch=16,cex=1)
points(ce.rho25.ci[,36],rep(a14,2),pch='|',cex=0.5)
lines(ce.rho25.ci[,36],rep(a14,2),lty=1,col='black')

points(ce.rho50.mean[36],a15,pch=16,cex=1)
points(ce.rho50.ci[,36],rep(a15,2),pch='|',cex=0.5)
lines(ce.rho50.ci[,36],rep(a15,2),lty=1,col='black')

abline(v=0,lty=3)

axis(2,at=c(-0.75,-0.5,-0.25,0,0.25,0.5,0.75),labels=c('-0.75','-0.5','-0.25','0','0.25','0.5' ,'0.75'),tick=T)



######    D/R   sigma  

par(mar=c(2, 4.5, 2, 0.5))

plot(ce.rho0.mean[51],a11,'p',pch=16,cex=1,ylim=c(-0.6,0.6),ylab=expression(sigma),xlab='',xlim=c(-0.05,0.5),yaxt='n',main='',cex.lab=1.5)
points(ce.rho0.ci[,51],rep(a11,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,51],c(a11,a11),lty=1,col='black')

points(ce.sigma25.mean[51],a12,pch=16,cex=1)
points(ce.sigma25.ci[,51],rep(a12,2),pch='|',cex=0.5)
lines(ce.sigma25.ci[,51],rep(a12,2),lty=1,col='black')

points(ce.sigma50.mean[51],a13,pch=16,cex=1)
points(ce.sigma50.ci[,51],rep(a13,2),pch='|',cex=0.5)
lines(ce.sigma50.ci[,51],rep(a13,2),lty=1,col='black')

points(ce.sigma75.mean[51],a14,pch=16,cex=1)
points(ce.sigma75.ci[,51],rep(a14,2),pch='|',cex=0.5)
lines(ce.sigma75.ci[,51],rep(a14,2),lty=1,col='black')

points(ce.sigma100.mean[51],a15,pch=16,cex=1)
points(ce.sigma100.ci[,51],rep(a15,2),pch='|',cex=0.5)
lines(ce.sigma100.ci[,51],rep(a15,2),lty=1,col='black')

abline(v=0,lty=3)
axis(2,at=c(a11,a12,a13,a14,a15 ),labels=c('0','0.25','0.5','0.75','1' ),tick=T)



######  F/M  sigma

par(mar=c(2, 4, 2, 0.5))

plot(ce.rho0.mean[36],a11,'p',pch=16,cex=1,ylim=c(-0.6,0.6),ylab='',xlab='',xlim=c(-0.05,0.5),yaxt='n',main='')
points(ce.rho0.ci[,36],rep(a11,2),pch='|',cex=0.5)
lines(ce.rho0.ci[,36],c(a11,a11),lty=1,col='black')

points(ce.sigma25.mean[36],a12,pch=16,cex=1)
points(ce.sigma25.ci[,36],rep(a12,2),pch='|',cex=0.5)
lines(ce.sigma25.ci[,36],rep(a12,2),lty=1,col='black')

points(ce.sigma50.mean[36],a13,pch=16,cex=1)
points(ce.sigma50.ci[,36],rep(a13,2),pch='|',cex=0.5)
lines(ce.sigma50.ci[,36],rep(a13,2),lty=1,col='black')

points(ce.sigma75.mean[36],a14,pch=16,cex=1)
points(ce.sigma75.ci[,36],rep(a14,2),pch='|',cex=0.5)
lines(ce.sigma75.ci[,36],rep(a14,2),lty=1,col='black')

points(ce.sigma100.mean[36],a15,pch=16,cex=1)
points(ce.sigma100.ci[,36],rep(a15,2),pch='|',cex=0.5)
lines(ce.sigma100.ci[,36],rep(a15,2),lty=1,col='black')

abline(v=0,lty=3)
axis(2,at=c(a11,a12,a13,a14,a15 ),labels=c('0','0.25','0.5','0.75','1' ),tick=T)



dev.off()








